package com.generator.sysManage.sectionManage.controller;


import com.generator.sysManage.sectionManage.pojo.DMDept;
import com.generator.sysManage.sectionManage.service.impl.DMService;
import com.generator.sysManage.userManage.pojo.*;
import com.generator.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * DMController
 * @author WJW
 * @Date .....
 * @version ......
 */
@Controller
@RequestMapping("/deptManage")
public class DMController {

    @Autowired
    private DMService dmService;
    public DMService getDmService() {
        return dmService;
    }
    public void setDmService(DMService dmService) {
        this.dmService = dmService;
    }



    //部门列表（分页）
    @RequestMapping("/deptList")
    public String userList(Model model, DMDept dmDept, PageModel<DMDept> pageModel){
        PageModel<DMDept> userList = dmService.getDMDeptListByFY(dmDept,pageModel);
        model.addAttribute("deptList",userList);
        return "sys/dept/deptList";
    }

    //搜索后的部门列表（分页）
    @RequestMapping("/deptList1")
    public String userList1(Model model, DMDept dmDept, PageModel<DMDept> pageModel, HttpSession session){
        DMDept dmDept1 = (DMDept) session.getAttribute("dmDept");
        PageModel<DMDept> userList = dmService.getDMDeptListByFY1(dmDept1,pageModel);
        model.addAttribute("deptList",userList);
        return "sys/dept/deptList1";
    }


    //根据条件查询所有省
    @RequestMapping("/provinceList")
    @ResponseBody
    public List<Province> provinceList(){
        return dmService.getProvinceList(new Province());
    }


    //根据条件查询所有市
    @ResponseBody
    @RequestMapping("/cityList")
    public List<City> cityList(City city){
        return dmService.getCityList(city);
    }


    //搜索
    @RequestMapping("/queryDepts")
    public String queryDepts(Model model, DMDept dmDept, HttpSession session,PageModel<DMDept> pageModel){
        session.setAttribute("dmDept",dmDept);
        PageModel<DMDept> userList = dmService.getDMDeptListByFY1(dmDept,pageModel);
        model.addAttribute("deptList",userList);
        return "sys/dept/deptList1";
    }


    //进入添加部门页面
    @RequestMapping("/deptAdd")
    public String addUser(){
        return "sys/dept/deptAdd";
    }


    //部门编号局部验证
    @RequestMapping("/dept_idYZ")
    @ResponseBody
    public String dept_idYZ(DMDept dmDept){
        List<DMDept> dmDeptList = dmService.getDMDeptListByDMDept(dmDept);
        if(dmDeptList!=null&&dmDeptList.size()==1){
            return "true";
        }
        return "false";
    }


    //添加部门
    @RequestMapping("/addDept1")
    @ResponseBody
    public String addDept1(DMDept dmDept){
        dmDept.setDept_state("0");
        Integer add = dmService.addDMDept(dmDept);
        if(add==1){
            return "true";
        }
        return "false";
    }



    //进入修改部门页面
    @RequestMapping("/deptUpdate")
    public String updateView(DMDept dmDept,Model model){
        return dmService.updateView(dmDept,model);
    }

    //进入搜索后修改部门页面
    @RequestMapping("/deptUpdate1")
    public String updateView1(DMDept dmDept,Model model){
        return dmService.updateView1(dmDept,model);
    }


    //修改部门
    @RequestMapping("/updateDept1")
    @ResponseBody
    public String updateUser1(DMDept dmDept){
        Integer updateDMDept = dmService.updateDMDept(dmDept);
        if(updateDMDept==1){
            return "true";
        }
        return "false";
    }


    //修改部门状态
    @RequestMapping("/updateState")
    public String updateStatu(DMDept dmDept,Model model){
        return dmService.updateState(dmDept,model);
    }


    //修改部门状态(搜索后的修改)
    @RequestMapping("/updateState1")
    public String updateStatu1(DMDept dmDept,HttpSession session,Model model){
        DMDept dmDept1 = (DMDept) session.getAttribute("dmDept");
        return dmService.updateState1(dmDept,model,dmDept1);
    }


}
